//
//  ReasonAndDesAlert.m
//  Rabbit
//
//  Created 阿帕 on 2019/12/19.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "ReasonAndDesAlert.h"

@interface ReasonAndDesAlert ()

@property(strong, nonatomic)UITextField *reasonTextField;

@property(strong, nonatomic)UITextField *desTextField;

@end

@implementation ReasonAndDesAlert

+ (ReasonAndDesAlert *)shared {
    
    static dispatch_once_t predicate;
    
    static ReasonAndDesAlert *objc;
    
    dispatch_once(&predicate, ^{
        
        objc = [[ReasonAndDesAlert alloc] init];
        
    });
    
    return objc;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI {
    
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    
    //黑色遮挡层
    UIView *barrierView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    barrierView.backgroundColor = RGBA(0, 0, 0, 0.5);
    [self addSubview:barrierView];
    
    [barrierView nyl_addTapGes:^{
        [self hidden];
    }];
    
    //确认信息底层白色View
    UIView *confirView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, ScreenW-100, 180)];
    confirView.tag = 100;
    confirView.center = self.center;
    confirView.cy_centerY-=120;
    confirView.backgroundColor = RGB(255, 255, 255);
    confirView.userInteractionEnabled = YES;
    [DCSpeedy dc_chageControlCircularWith:confirView AndSetCornerRadius:5 SetBorderWidth:0 SetBorderColor:nil canMasksToBounds:YES];
    [self addSubview:confirView];
    
    UILabel *reasonText = [[UILabel alloc] initWithFrame:CGRectMake(26, 34, 120, 20)];
    reasonText.text = @"取消原因";
    reasonText.textColor = Dark_TextColor;
    reasonText.font = font(18);
    [confirView addSubview:reasonText];
    
    _reasonTextField = [[UITextField alloc] initWithFrame:CGRectMake(reasonText.left, reasonText.cy_bottom + 10, confirView.width - 26*2, 40)];
    _reasonTextField.font = font(14);
    _reasonTextField.text = @"";
    _reasonTextField.textColor = Light_TextColor;
    _reasonTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    _reasonTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _reasonTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写取消原因" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    [_reasonTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [DCSpeedy dc_setUpAcrossPartingLineWith:_reasonTextField WithColor:Theme_ColorValue];
    [confirView addSubview:_reasonTextField];
    
    //有描述
    UILabel *desText = [[UILabel alloc] initWithFrame:CGRectMake(reasonText.left, _reasonTextField.cy_bottom + 20, 120, 20)];
    desText.tag = 150;
    desText.hidden = YES;
    desText.text = @"处理描述";
    desText.textColor = Dark_TextColor;
    desText.font = font(18);
    [confirView addSubview:desText];
    
    _desTextField = [[UITextField alloc] initWithFrame:CGRectMake(reasonText.left, desText.cy_bottom + 10, confirView.width - 26*2, 40)];
    _desTextField.font = font(14);
    _desTextField.hidden = YES;
    _desTextField.text = @"";
    _desTextField.textColor = Light_TextColor;
    _desTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    _desTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _desTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写描述内容" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    [_desTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [DCSpeedy dc_setUpAcrossPartingLineWith:_desTextField WithColor:Theme_ColorValue];
    [confirView addSubview:_desTextField];

    
    NSArray *titles = @[@"取消", @"确定"];
    
    for (int i=0; i<2; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i + 300;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = font(16);
        [button setTitleColor:i==0?Theme_ColorValue:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:i==0?[UIColor whiteColor]:Theme_ColorValue];
        cut(button, BTN_BORDER_RADIUS);
        if (i==0) {
            cutBorder(button, 1, @"#2C7CF6")
        }
        [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
        [confirView addSubview:button];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
    
}

- (void)setAlertType:(NSInteger)alertType {
    _alertType = alertType;
    if (_alertType == 1) {
        //取消
        _reasonTextField.enabled = YES;
    } else {
        //确认取消
        _reasonTextField.enabled = NO;
    }
}

- (void)setCancelReasons:(NSString *)cancelReasons {
    if ([DCSpeedy isBlankString:cancelReasons] == NO) {
        _cancelReasons = cancelReasons;
        _reasonTextField.text = _cancelReasons;
    }
}

- (void)btnEventClick:(UIButton *)button {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(ReturnCancelReasonDes:andDes:andBtnIndex:)]) {
        [self.delegate ReturnCancelReasonDes:_reasonTextField.text andDes:_desTextField.text andBtnIndex:button.tag];
    }
    
}

- (void)textFieldChanged:(UITextField *)textField {
    
    
}

- (void)show {
    
    UIView *confirView = [self viewWithTag:100];
    UILabel *desText = [self viewWithTag:150];
    UIButton *leftButton = [self viewWithTag:300];
    
    if (_alertType == 1) {
        
        [leftButton setTitle:@"取消" forState:UIControlStateNormal];
        confirView.cy_height = 180;
        desText.hidden = YES;
        _desTextField.hidden = YES;
        
        for (int i=0; i<2; i++) {
            UIButton *button = [self viewWithTag:300 + i];
            button.frame = i==0 ? CGRectMake(30, _reasonTextField.cy_bottom + 20, 90, 34) : CGRectMake(confirView.cy_width - 90 - 30, _reasonTextField.cy_bottom + 20, 90, 34);
        }
        
    } else {
        
        [leftButton setTitle:@"拒绝" forState:UIControlStateNormal];
        confirView.cy_height = 280;
        desText.hidden = NO;
        _desTextField.hidden = NO;
        for (int i=0; i<2; i++) {
            UIButton *button = [self viewWithTag:300 + i];
            button.frame = i==0 ? CGRectMake(30, _desTextField.cy_bottom + 20, 90, 34) : CGRectMake(confirView.cy_width - 90 - 30, _desTextField.cy_bottom + 20, 90, 34);
        }
        
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)hidden {
    
    [self endEditing:YES];
    
    _reasonTextField.text = @"";
    _desTextField.text = @"";
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0;
    }];
}

@end
